package com.schoolCommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.schoolCommunity.entity.ForIndexMessage;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.mapper.ForIndexMapper;
import com.schoolCommunity.mapper.ForIndexMessageMapper;
import com.schoolCommunity.service.ForIndexMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolCommunity.util.InsertES;
import com.schoolCommunity.util.RedisUtil;
import com.schoolCommunity.util.UploadPlacePicture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-10-08
 */
@Service
public class ForIndexMessageServiceImpl extends ServiceImpl<ForIndexMessageMapper, ForIndexMessage> implements ForIndexMessageService {

    @Resource
    private ForIndexMessageMapper forIndexMessageMapper;
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public Information DeleteMessage(long id) {
        Information information = new Information();

        int i = forIndexMessageMapper.deleteById(id);
        if (i > 0) {
            information.setMessage("留言删除成功").setCode(200);
        } else {
            information.setMessage("留言删除失败").setCode(404);
        }
        return information;
    }

    @Override
    public Information getMessageForIndex(long id) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("for_index_id", id);
        List list = forIndexMessageMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            information.setData(list).setMessage("留言获取成功").setCode(200);
        } else {
            information.setMessage("留言获取失败").setCode(404);
        }
        return information;
    }

    @Override
    public Information insertMessage(ForIndexMessage forIndexMessage) {
        Information information = new Information();
        int insert = forIndexMessageMapper.insert(forIndexMessage);
        if (insert > 0) {
            information.setCode(200).setMessage("留言添加成功");
        } else {
            information.setCode(200).setMessage("留言添加失败");
        }
        return information;
    }

    //点赞某条留言
    @Override
    public Information AddLovesForIndexMessage(long id, long memberId) {
        Information information = new Information();
        //先判断是否存在redis
        String redisKey = "AddLovesForIndexMessage:" + Integer.toString((int) id) + ":" + Integer.toString((int) memberId);
        if (redisUtil.CheckRedis(redisKey)) {
            //存在key
            information.setMessage("每条留言一天只能点赞一次哦").setCode(404);
        } else {
            //不存在key
            //执行点赞
            Integer integer = forIndexMessageMapper.AddLovesForIndexMessage(id);
            if (integer > 0) {
                //数据库已经点赞成功
                //redis记载
                redisUtil.InsertRedis(redisKey, redisKey);
                information.setMessage("点赞留言成功").setCode(200);
            } else {
                information.setMessage("点赞留言失败").setCode(404);
            }
        }


        return information;
    }
}
